package com.dundun.SSO.controller.manage;

import com.dundun.SSO.core.base.BaseController;
import com.dundun.SSO.manage.ISsoUserAccountService;
import com.dundun.SSO.manage.impl.SsoUserAccountServiceImpl;
import com.dundun.SSO.manage.pojo.SsoUser;
import com.dundun.SSO.manage.pojo.SsoUserAccount;
import com.dundun.SSO.pojo.ResponseBean;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**********************************************************
 * 版权所有：搜影科技 保留所有权利
 * 创建日期: 2017/5/24 0024 16:13
 * 创建作者: gezhangkai
 * 版    本:
 * 功    能:
 * 最后修改时间:
 * 修改记录:
 ***********************************************************/
@Controller
@RequestMapping("/sso/userAccount")
public class SsoUserAccountController extends BaseController {
    Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private ISsoUserAccountService ssoUserAccountService;

    @RequestMapping("/add.json")
    @ResponseBody
    public ResponseBean addUserAccount(@RequestBody SsoUserAccount ssoUserAccount){
        if(StringUtils.isBlank(ssoUserAccount.getAccount())){
            return ResponseBean.parameterError("账号不能为空");
        }
        if(StringUtils.isBlank(ssoUserAccount.getPassword())){
            return ResponseBean.parameterError("密码不能为空");
        }
        SsoUserAccount ssoUserAccountTemp = new SsoUserAccount();
        ssoUserAccountTemp.setAccount(ssoUserAccount.getAccount());
        if(ssoUserAccountService.selectCount(ssoUserAccountTemp) > 0){
            return ResponseBean.parameterError("账号已存在");
        }
        ssoUserAccountService.saveUser(ssoUserAccount);
        return ResponseBean.success(null,null);
    }

    @RequestMapping("/updatePassword.json")
    @ResponseBody
    public ResponseBean updatePassword(@RequestBody String account,@RequestBody String password,@RequestBody String oldPassword){
        if(StringUtils.isBlank(account)){
            return ResponseBean.parameterError("账号不能为空");
        }
        if(StringUtils.isBlank(oldPassword)){
            return ResponseBean.parameterError("原始密码不能为空");
        }
        if(StringUtils.isBlank(password)){
            return ResponseBean.parameterError("新密码不能为空");
        }
        SsoUserAccount ssoUserAccount = new SsoUserAccount();
        ssoUserAccount.setAccount(account);
        ssoUserAccount = ssoUserAccountService.selectOne(ssoUserAccount);
        if(ssoUserAccount == null || !ssoUserAccount.getPassword().equals(oldPassword)){
            return ResponseBean.parameterError("账号或密码错误");
        }
        ssoUserAccount.setPassword(password);
        ssoUserAccountService.updateNotNull(ssoUserAccount);
        return ResponseBean.success(null,null);
    }

    @RequestMapping("/delete.json")
    @ResponseBody
    public ResponseBean deleteAccount(@RequestBody Long[] ids){
        if(ids == null){
            return ResponseBean.parameterError("id不能为空");
        }
        String msg = "";
        for(Long id : ids){
            if(ssoUserAccountService.delete(id) < 1){
                msg += "," + id;
            }
        }
        if(StringUtils.isBlank(msg)){
            return ResponseBean.success("删除成功",null);
        }else{
            return ResponseBean.success(String.format("id为%s的数据未能删除",msg.substring(1)),null);
        }
    }

    @RequestMapping("/updateAccount.json")
    @ResponseBody
    public ResponseBean updateAccount(@RequestBody SsoUserAccount ssoUserAccount){
        try {
            if (ssoUserAccount.getId() == null) {
                return ResponseBean.parameterError("id不能为空");
            }
            if (StringUtils.isBlank(ssoUserAccount.getAccount()) || StringUtils.isBlank(ssoUserAccount.getPassword())) {
                return ResponseBean.parameterError("账号或密码不能为空");
            }
            if (ssoUserAccountService.updateByPrimaryKey(ssoUserAccount) > 0) {
                return ResponseBean.success(null, null);
            } else {
                return ResponseBean.unknowError("修改失败", null);
            }
        }catch (Exception e){
            return ResponseBean.unknowError(e.getMessage(),null);
        }
    }
}
